package com.jmu.posms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.jmu.posms.common.Result;
import com.jmu.posms.dto.OrderFormDto;
import com.jmu.posms.dto.OrderInputDto;
import com.jmu.posms.dto.OrderDataDto;
import com.jmu.posms.dto.OrderOutputDto;
import com.jmu.posms.exception.ServiceException;
import com.jmu.posms.pojo.Address;
import com.jmu.posms.pojo.Order;
import com.jmu.posms.pojo.User;
import com.jmu.posms.service.AddressService;
import com.jmu.posms.service.NewspaperService;
import com.jmu.posms.service.OrderService;
import com.jmu.posms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Resource
    OrderService orderService;
    @Resource
    UserService userService;

    @PostMapping("/insert")
    public Result add(@RequestBody OrderInputDto inputDto){
        String oderId = orderService.add(inputDto);

        return Result.success(oderId,"插入成功");
    }

    @GetMapping("/get/page")
    public Result get(Integer page,Integer pageSize,String userName,String newsName){

        Set<OrderDataDto> orderDataDto = orderService.find(userName, newsName);

        return getResult(page, pageSize, orderDataDto);
    }

    @GetMapping("/get/page/my")
    public Result get(Integer page,Integer pageSize,String newsName){
        Set<OrderDataDto> orderDataDto = orderService.findByNews(newsName);

        return getResult(page, pageSize, orderDataDto);
    }

    private Result getResult(Integer page, Integer pageSize, Set<OrderDataDto> orderDataDto) {
        List<OrderDataDto> orderList = new ArrayList<>(orderDataDto);

        List<OrderDataDto> outputDtoList = new ArrayList<>();

        for (int i = (page-1)*pageSize; i < orderList.size()&&i<page*pageSize; i++) {
            outputDtoList.add(orderList.get(i));
        }

        Double amount = 0.0;
        Integer allNum = 0;
        for (OrderDataDto dataDto : orderList) {
            amount += dataDto.getAmount();
            allNum += dataDto.getOrderForm().getNum();
        }

        return Result.success(new OrderOutputDto(outputDtoList,orderList.size(),amount,allNum),"查询成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        Order order = orderService.getById(id);
        String userId = order.getUserId();
        User user = userService.getById(userId);
        if(user.getRoleId()!="1"){
            //该用户不是管理员 ， 校验是否在删除他本人的订单
            //获取当前登录用户的信息
            Subject subject = SecurityUtils.getSubject();
            User usermy = (User) subject.getPrincipal();
            if (usermy.getId().equals(userId)) {
                throw new ServiceException("403","没有权限删除该条订单！");
            }
        }
        try{
            orderService.removeById(id);
        }catch (Exception e){
            throw new ServiceException("600","删除失败");
        }
        return Result.success(null,"删除成功");
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> ids){
        try{
            orderService.removeBatchByIds(ids);
        }catch (Exception e){
            throw new ServiceException("600","删除失败");
        }
        return Result.success(null,"删除成功");
    }
}
